package com.fulihui.wgj.dal.impala.mapper;

import com.fulihui.wgj.dal.impala.domain.WxUserInfo;
import com.fulihui.wgj.dal.impala.domain.WxUserInfoSend;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface UserInfoMapper {

    String COL = "openid,unionid,country,province,city,nickname,sex,language,headimgurl,subscribe,subscribe_time," +
            "groupid,tag,remark,qr_scene_str,os,is_td,has_click_link,has_click_mini_program";

    String GAME_SEND_COL = "a.openid,a.nickname";

    /**
     * 查询用户-用于发送
     *
     * @param table     表名
     * @param condition 条件
     * @return
     */
    @Select("select openid,nickname from ${table} ${queryCondition}")
    List<WxUserInfoSend> querySendUser(@Param("table") String table, @Param("queryCondition") String condition);

    /**
     * 查询用户- 用于游戏发送
     *
     * @param table     用户表名
     * @param condition 字段条件
     * @return
     */
    @Select("SELECT DISTINCT " + GAME_SEND_COL + " FROM ${table} ${condition}")
    List<WxUserInfoSend> querySendUser4Game(@Param("table") String table, @Param("condition") String condition);

    /**
     * 计算发送数量
     *
     * @param table
     * @param condition
     * @return
     */
    @Select("select count(*) from ${table}  ${queryCondition}")
    Long countSendUser(@Param("table") String table, @Param("queryCondition") String condition);

    /**
     * 计算人数--针对游戏标签
     * SELECT count(*) FROM u_wxda9a5c51a81b7839 AS a LEFT OUTER JOIN user_game_behavior AS b ON a.openid = b.open_id WHERE b.app_id = 'wxda9a5c51a81b7839';
     *
     * @param table
     * @param condition
     * @return
     */
    @Select("SELECT count(DISTINCT a.openid) FROM ${table} ${condition}")
    Long count4Game(@Param("table") String table, @Param("condition") String condition);

    /**
     * 查询微信用户列表
     *
     * @param table     表名
     * @param condition 条件
     * @return
     */
    @Select("SELECT " + COL + " FROM ${table} WHERE ${condition}")
    List<WxUserInfo> query(@Param("table") String table, @Param("condition") String condition);

    /**
     * 根据openid查询用户
     *
     * @param table  表名
     * @param openid 用户openid
     * @return
     */
    @Select("SELECT " + COL + " FROM ${table} WHERE #{openid}")
    WxUserInfo queryByOpenid(@Param("table") String table, @Param("openid") String openid);

    /**
     * 查询数量
     *
     * @param table     表名
     * @param condition 条件
     * @return
     */
    @Select("SELECT count(*) FROM ${table} WHERE ${condition}")
    Long count(@Param("table") String table, @Param("condition") String condition);

    /**
     * 创建微信用户表
     *
     * @param appId 公众号appId
     */
    void createTableWxUser(@Param("appId") String appId);

    /**
     * 判断用户是否已存在
     *
     * @param table
     * @param openId
     * @return
     */
    String userIsExist(@Param("table") String table, @Param("openId") String openId);

    /**
     * 保存用户
     *
     * @param wxUserInfo
     * @param appId
     */
    void saveUser(@Param("model") WxUserInfo wxUserInfo, @Param("appId") String appId);

    /**
     * 更新用户
     *
     * @param wxUserInfo 必须包含openid
     * @param appId
     */
    void updateByOpenId(@Param("model") WxUserInfo wxUserInfo, @Param("appId") String appId);
}